package com.rf.richfitwheel.admin.sys.controller;

import com.rf.richfitwheel.admin.annotation.UserOperateLog;
import com.rf.richfitwheel.admin.contants.UrlConstant;
import com.rf.richfitwheel.admin.sys.model.Dictionary;
import com.rf.richfitwheel.admin.sys.model.SysCurInfo;
import com.rf.richfitwheel.admin.sys.service.SysCurInfoService;
import com.rf.richfitwheel.admin.sys.service.SysDicService;
import com.rf.richfitwheel.common.contants.CommonServiceError;
import com.rf.richfitwheel.common.exception.BusinessException;
import com.rf.richfitwheel.common.utils.PageUtils;
import com.rf.richfitwheel.common.utils.R;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(UrlConstant.WEB_ADMIN_URL + "/curinfo")
public class SysCurInfoController {
    private static Logger log = LoggerFactory.getLogger(SysCurInfoController.class);

    @Autowired
    private SysCurInfoService sysCurInfoService;


    /**
     * 所有配置列表
     */
    @GetMapping("/queryPageList")
    public R queryPageList(String page, String limit, String dataForm) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        params.put("limit", limit);
        params.put("dataForm", dataForm);
        PageUtils pages = sysCurInfoService.queryPage(params);
        return R.ok().put("page", pages);
    }

    /**
     * 所有配置列表
     */
    @GetMapping("/list")
    public R list() {
        List<SysCurInfo> list = sysCurInfoService.getCurList();
        return R.ok().put("list", list);
    }

    /**
     * 配置信息
     *
     * @throws Exception
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") String id) throws Exception {
        SysCurInfo sysCurInfo = sysCurInfoService.selectById(id);
        return R.ok().put("sysCurInfo", sysCurInfo);
    }

    /**
     * 保存配置
     */
    @UserOperateLog(module = "系统功能", methods = "save", description = "新增汇率信息")
    @PostMapping("/save")
    public R save(@RequestBody SysCurInfo sysCurInfo) {
        try {
            sysCurInfoService.save(sysCurInfo);
            return R.ok();
        } catch (BusinessException e){
            return R.error(e.getMessage());
        } catch (Exception e) {
            log.error(CommonServiceError.SERVICE_SAVE.getMsg() + "币种" + CommonServiceError.SERVICE_FAILURE.getMsg(), e);
            return R.error(CommonServiceError.SERVICE_SAVE.getMsg() + "币种" + CommonServiceError.SERVICE_FAILURE.getMsg());
        }
    }

    /**
     * 修改配置
     */
    @UserOperateLog(module = "系统功能", methods = "update", description = "修改汇率信息")
    @PostMapping("/update")
    public R update(@RequestBody SysCurInfo sysCurInfo) {
        try {
            sysCurInfoService.update(sysCurInfo);
            return R.ok();
        } catch (BusinessException e){
            return R.error(e.getMessage());
        } catch (Exception e) {
            log.error(CommonServiceError.SERVICE_MODIFY.getMsg() + "币种" + CommonServiceError.SERVICE_FAILURE.getMsg(), e);
            return R.error(CommonServiceError.SERVICE_MODIFY.getMsg() + "币种" + CommonServiceError.SERVICE_FAILURE.getMsg());
        }
    }

    /**
     * 删除配置
     */
    @UserOperateLog(module = "系统功能", methods = "delete", description = "删除汇率信息")
    @PostMapping("/delete")
    public R delete(@RequestBody String[] ids) {
        sysCurInfoService.deleteBatch(ids);

        return R.ok();
    }


    @GetMapping("/getConvertAmtByCurNo/{amt}/{cur}")
    public BigDecimal getConvertAmtByCurNo(@PathVariable("amt") BigDecimal amt, @PathVariable("cur") String cur) throws Exception {
        BigDecimal convertAmt = sysCurInfoService.getConvertAmtByCurNo(amt,cur);
        return  convertAmt;
    }

}
